package com.heyige.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyige.common.CommResponse;
import com.heyige.dto.QuotaPackageDto;
import com.heyige.entity.QuotaPackage;
import com.heyige.facade.QuotaPackageFacade;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/quotaPackages")
@RequiredArgsConstructor
@Tag(name = "次数包管理", description = "次数包相关接口")
public class QuotaPackageController {
    
    private final QuotaPackageFacade quotaPackageFacade;
    
    /**
     * 获取所有次数包
     */
    @GetMapping
    @Operation(summary = "获取所有次数包", description = "获取系统中的所有次数包列表")
    public CommResponse<List<QuotaPackage>> getAllQuotaPackages() {
        IPage<QuotaPackage> page = quotaPackageFacade.getAllQuotaPackages(0, 100);
        return CommResponse.ok(page.getRecords());
    }
    
    /**
     * 获取次数包详情
     */
    @GetMapping("{id}")
    @Operation(summary = "获取次数包详情", description = "根据ID获取次数包详情")
    public CommResponse<QuotaPackage> getQuotaPackage(
            @Parameter(description = "次数包ID", required = true, example = "1") @PathVariable Long id) {
        QuotaPackage quotaPackage = quotaPackageFacade.getQuotaPackage(id);
        return CommResponse.ok(quotaPackage);
    }
    
    /**
     * 创建次数包订单
     */
    @PostMapping("orders")
    @Operation(summary = "创建次数包订单", description = "创建购买次数包的订单")
    public CommResponse<QuotaPackageDto.OrderResponse> createQuotaOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "创建订单请求参数") @Valid @RequestBody QuotaPackageDto.CreateOrderRequest request) {
        QuotaPackageDto.OrderResponse response = quotaPackageFacade.createQuotaOrder(userId, request);
        return CommResponse.ok(response);
    }
    
    /**
     * 支付次数包订单
     */
    @PostMapping("orders/{orderId}/pay")
    @Operation(summary = "支付次数包订单", description = "支付指定的次数包订单")
    public CommResponse<QuotaPackageDto.OrderResponse> payQuotaOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId,
            @Parameter(description = "支付请求参数") @Valid @RequestBody QuotaPackageDto.PayRequest request) {
        QuotaPackageDto.OrderResponse response = quotaPackageFacade.payQuotaOrder(orderId, userId, request);
        return CommResponse.ok(response);
    }
    
    /**
     * 取消次数包订单
     */
    @PostMapping("orders/{orderId}/cancel")
    @Operation(summary = "取消次数包订单", description = "取消指定的次数包订单")
    public CommResponse<Void> cancelQuotaOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId) {
        quotaPackageFacade.cancelQuotaOrder(orderId, userId);
        return CommResponse.ok();
    }
    
    /**
     * 获取用户的次数包订单列表
     */
    @GetMapping("orders/my")
    @Operation(summary = "获取用户次数包订单", description = "分页获取用户的次数包订单列表")
    public CommResponse<IPage<QuotaPackageDto.OrderResponse>> getUserQuotaOrders(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<QuotaPackageDto.OrderResponse> orders = quotaPackageFacade.getUserQuotaOrders(userId, page, size);
        return CommResponse.ok(orders);
    }
    
    /**
     * 获取订单详情
     */
    @GetMapping("orders/{orderId}")
    @Operation(summary = "获取订单详情", description = "根据订单ID获取次数包订单详情")
    public CommResponse<QuotaPackageDto.OrderResponse> getQuotaOrder(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "订单ID", required = true, example = "1") @PathVariable Long orderId) {
        QuotaPackageDto.OrderResponse response = quotaPackageFacade.getQuotaOrder(orderId, userId);
        return CommResponse.ok(response);
    }
}